package com.cndw.kungfu.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cndw.kungfu.comm.tencent.LogFieldType;
import com.cndw.kungfu.comm.tencent.QQDC;
import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsCost;
import com.cndw.kungfu.config.ConstantsLevel;
import com.cndw.kungfu.config.ConstantsLog;
import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.config.ConstantsRadio;
import com.cndw.kungfu.config.GoodsEffectConfig;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.event.once.PlayerStateTask;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.NumberProcesser;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.battle.Battle;
import com.cndw.kungfu.model.battle.FighterMonster;
import com.cndw.kungfu.model.pve.CityBattlePlayer;
import com.cndw.kungfu.model.pve.CityDefensesBattle;
import com.cndw.kungfu.model.pve.FighterMonsterAi;
import com.cndw.kungfu.model.sys.ChatMain;
import com.cndw.kungfu.model.sys.LogServer;
import com.cndw.kungfu.vo.EventNotice;
import com.cndw.kungfu.vo.battlevo.FighterAiVO;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

/**
 * 一些 pve活动的入口
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2012-6-20 下午3:57:34
 */
@Controller
public class PveController {

	/**
	 * 襄阳保卫战进入加载
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage defenseLoad(Session session) {

		Collection<FighterMonsterAi> monsterAis = CityDefensesBattle.getFighters();
		List<FighterAiVO> currentDeploys = new ArrayList<FighterAiVO>(monsterAis.size());
		int diff = TimeProcesser.getUnixTime() - CityDefensesBattle.monsterRefreshTime;
		for (FighterMonsterAi e : monsterAis) {
			FighterAiVO ve = e.toVO();
			ve.setX(e.getX() - (2500 * diff / CityDefensesBattle.monsterLifeTime));
			currentDeploys.add(ve);
		}
		int buffAdd = 0, dam = 0, reviveTime = 0;
		CityBattlePlayer cbp = CityDefensesBattle.playerGet(session.getUid());
		if (cbp != null) {
			buffAdd = cbp.getBuff();
			dam = cbp.getDam();
			reviveTime = cbp.getDead();
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("monsterAis", currentDeploys);
		map.put("bossHp", CityDefensesBattle.getBossHp()==0 ? 100 : CityDefensesBattle.getBossHp());
		map.put("buffAdd", buffAdd);
		map.put("dam", dam);
		map.put("reviveTime", reviveTime);
		map.put("currentStartTime", CityDefensesBattle.getCurrentStartTime() + CityDefensesBattle.readyTime);
		map.put("monsterOverTime", CityDefensesBattle.monsterRefreshTime + CityDefensesBattle.monsterLifeTime);
		map.put("monsterLifeTime", CityDefensesBattle.monsterLifeTime);
		map.put("orderList", CityDefensesBattle.orderDam());
		map.put("monsterHpAll", CityDefensesBattle.getMonsterHpAll());
		
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		QQDC.getInstance().logClick(player, LogFieldType.cityDefense);

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 襄阳保卫战,杀怪
	 * 
	 * @param session
	 * @param uniqueId
	 * @return
	 */
	public ResponseMessage defenseBattle(Session session, long uniqueId) {

		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (player.getTeam() != null) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("team.err.controlontem"));
		}else if (player.getLevel() < ConstantsLevel.lv30CityBattle) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.level",ConstantsLevel.lv30CityBattle));
		}

		FighterMonsterAi ai = CityDefensesBattle.getFighterAi(uniqueId);
		if (ai == null) {
			return null;
		}
		FighterMonster fighter = ai.getFighter();
		CityBattlePlayer cbp = CityDefensesBattle.playerGet(session.getUid());
		if (cbp != null && cbp.getDead() > TimeProcesser.getUnixTime()) {
			return null;
		}

		Battle battle = new Battle(player.getLikedFighter(cbp == null ? 0 : cbp.getBuff()), fighter);
		battle.execBattle(Constants.battle_noaward);
		battle.broadcast(player);
		battle.clear();

		CityDefensesBattle.playerDamAdd(session.getUid(), battle.getSourceDam());// 把血量记录进去
		int dam = CityDefensesBattle.playerGet(session.getUid()).getDam();
		if (fighter.getHp() <= 0 && fighter.getSourceObject().getBoss() == 5) {
			CityDefensesBattle.akPlayerSet(session.getUid());// 做最后一击
		}
		int reviveTime = 0;
		if (battle.getWiner() != 1) {
			reviveTime = TimeProcesser.getUnixTime() + battle.getCostTime() + 15;
			CityDefensesBattle.playerGet(session.getUid()).killDeath(reviveTime);
			player.setX(300);
			player.setY(NumberProcesser.mtRand(482 - 100, 482 + 100));
		}

		EventNotice eventNotice = new EventNotice(ConstantsRadio.cityBattle, ConstantsRadio.cityBattleMonsterHp);
		eventNotice.put("uniqueId", fighter.getUniqueId());
		eventNotice.put("hp", fighter.getHp());
		eventNotice.put("playerId", session.getUid());
		if (CityDefensesBattle.isOderTop(dam)) {
			eventNotice.put("orderList", CityDefensesBattle.orderDam());
			eventNotice.put("monsterHpAll", CityDefensesBattle.getMonsterHpAll());
		}
		eventNotice.broadcastAllScene(CityDefensesBattle.sceneId);// 广播全线人

		if (battle.getSourceDam() > 0) {//打出血才记录参加襄阳保卫战活动
			player.getDataInfo().alterExpRet(Constants.achiveRet_cb);
		}
		
		if (fighter.getHp() <= 0) {// 移除这个战斗对象
			CityDefensesBattle.removeFighterMonsterAi(fighter.getUniqueId(), battle.getCostTime());
		}
		player.stateAdd(Constants.battle_pve);
		Context.getPlayerServer().broadcaseState(player);
		new PlayerStateTask(Constants.battle_pve, battle.getCostTime(), player);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dam", dam);
		map.put("reviveTime", reviveTime);
		LogServer.recordLog(player, ConstantsLog.battle, 40304, dam);
		QQDC.getInstance().logClick(player, LogFieldType.cityDefenseBattle);
		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 给boss加血
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage defenseAddBossHp(Session session) {
		if (CityDefensesBattle.isSignEnd()) {
			return null;
		} else if (CityDefensesBattle.getBossHp() >= 100 || CityDefensesBattle.getBossHp() == 0) {
			return null;
		}

		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (player.getGoldAll() < CityDefensesBattle.bossHpGold) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		} else if (CityDefensesBattle.getBossHpAddTimes() >= 100) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("pve.city.hpdd"));
		}
		player.alterGoldAll(-CityDefensesBattle.bossHpGold);
		CityDefensesBattle.bossHpAlter(1, true);

		int buffNow = CityDefensesBattle.getBuffAdd(session.getUid());
		boolean suc = NumberProcesser.isWinning(10);

		if (suc) {
			buffNow += 5;
			CityDefensesBattle.playerBuffSet(session.getUid(), buffNow);
		}

		ChatMain.systemMessage(LanguageLib.get(suc ? "pve.city.hp2" : "pve.city.hp1", player.getPlayerName()));

		EventNotice eventNotice = new EventNotice(ConstantsRadio.cityBattle, ConstantsRadio.cityBattleBossHp);
		eventNotice.put("bossHp", CityDefensesBattle.getBossHp());
		eventNotice.broadcastAllScene(CityDefensesBattle.sceneId);

		LogServer.costEventLog(session.getUid(), CityDefensesBattle.bossHpGold, ConstantsCost.cityBattleBossHp);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());
		map.put("bossHp", CityDefensesBattle.getBossHp());
		map.put("suc", suc);
		map.put("buffAdd", buffNow);
		LogServer.recordLog(player, ConstantsLog.battle, 40301, CityDefensesBattle.bossHpGold);

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 襄阳守卫战 buff
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage defenseBuff(Session session) {
		if (CityDefensesBattle.getBossHp() == 0) {
			return null;
		}else if (CityDefensesBattle.isSignEnd()) {
			return null;
		}
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		int haveTool = player.getPlayerBoxs().getStackNum(GoodsEffectConfig.buffGoodsId);
		boolean costTool =  haveTool > 0 ;
		if (!costTool && player.getGoldAll() < ConstantsModel.cityBattleBuffGold) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		int buffNow = CityDefensesBattle.getBuffAdd(session.getUid());
		if (buffNow >= ConstantsModel.cityBattleBuffMax) {
			return null;
		}
		if (costTool) {
			player.getPlayerBoxs().costGoods(GoodsEffectConfig.buffGoodsId, 1);
			haveTool --;
		}else {
			player.alterGoldAll(-ConstantsModel.cityBattleBuffGold);
			player.commitSync();
			LogServer.costEventLog(session.getUid(), ConstantsModel.cityBattleBuffGold, ConstantsCost.cityBattleBuff);
		}
		buffNow += ConstantsModel.cityBattleBuffAdd;
		CityDefensesBattle.playerBuffSet(session.getUid(), buffNow);
		
		EventNotice notice = new EventNotice(ConstantsRadio.battle, ConstantsRadio.battle_BuffAdd);
		notice.put("playerId", player.getPlayerId());
		notice.put("buffNow", buffNow);
		notice.broadcastScene(player);
		
		LogServer.recordLog(player, ConstantsLog.battle, 40302, costTool ? 0 : ConstantsModel.cityBattleBuffGold, buffNow, costTool, haveTool);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());
		map.put("buffAdd", buffNow);
		map.put("suc", true);

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 复活
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage defenseRevive(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		CityBattlePlayer cbp = CityDefensesBattle.playerGet(session.getUid());
		if (cbp == null) {
			return null;
		}
		if (player.getGoldAll() < CityDefensesBattle.reviveGold) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		cbp.setDead(0);// 重置死亡时间
		cbp.revive();

		player.alterGoldAll(-CityDefensesBattle.reviveGold);
		player.fullBloodMagic();// 满血慢蓝
		if (player.getDataInfo().getPet() != null) {
			player.getDataInfo().getPet().fullHpMp();
		}
		player.commitSync();
		LogServer.costEventLog(session.getUid(), CityDefensesBattle.reviveGold, ConstantsCost.cityBattleRevive);
		LogServer.recordLog(player, ConstantsLog.battle, 40303, CityDefensesBattle.reviveGold);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());
		return ResponseMessage.getSuccMessage(map);
	}
}
